package com.yjd.bi.statistic.control;

import com.yjd.bi.comm.utils.BiDateUtil;
import com.yjd.bi.report.model.BiDatabaseInfoModel;
import com.yjd.bi.report.service.IBiDatabaseInfoService;
import com.yjd.bi.report.util.BiUtil;
import com.yjd.comm.annotation.DateString;
import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.NumberUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * 自动投标统计
 * Created by yt on 2016/12/15.
 */
@Controller
@Scope("prototype")
@RequestMapping("/autoinvest")
public class AutoInvestControl extends BaseControl {

    public final static String GET_AUTO_INVEST_COUNT = "select (select count(*) from yjd_user_auto_invest a where addtime < ?) s1,(select count(*) from yjd_user_auto_invest a,yjd_user b where a.addtime < ? and a.user_id = b.user_id and b.bank_depo = 0) s2,(select count(*) from yjd_user_auto_invest a,yjd_user b where a.addtime < ? and a.user_id = b.user_id and b.bank_depo = 1) s3";
    public final static String GET_INVEST_COUNT = "select count(DISTINCT(user_id)) from yjd_invest where channel = 8 and addtime between ? and ? ";
    public final static String GET_INVEST_PRICE = "select ifnull(sum(price),0.00) from yjd_invest where channel = 8 and addtime between ? and ?";
    public final static String GET_INVEST_COUNT_1 = "select count(DISTINCT(a.user_id)) from yjd_invest a,yjd_user b where a.user_id = b.user_id and b.bank_depo = 0 and a.channel = 8 and a.addtime between ? and ? ";
    public final static String GET_INVEST_PRICE_1 = "select ifnull(sum(a.price),0.00) from yjd_invest a,yjd_user b where a.user_id = b.user_id and b.bank_depo = 0 and a.channel = 8 and a.addtime between ? and ?";
    public final static String GET_INVEST_COUNT_2 = "select count(DISTINCT(a.user_id)) from yjd_invest a,yjd_user b where a.user_id = b.user_id and b.bank_depo = 1 and a.channel = 8 and a.addtime between ? and ? ";
    public final static String GET_INVEST_PRICE_2 = "select ifnull(sum(a.price),0.00) from yjd_invest a,yjd_user b where a.user_id = b.user_id and b.bank_depo = 1 and a.channel = 8 and a.addtime between ? and ?";

    /**
     * 数据库信息服务
     */
    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    private IBiDatabaseInfoService biDatabaseInfoService;

    @RequestMapping("/index")
    public String index(@DateString String start, @DateString String end) throws Exception {
        request.setAttribute("start", "2013-12-21");
        request.setAttribute("now", BiDateUtil.initBiDate());
        return "autoinvest.index";
    }

    /**
     * 开启过自动投标人数
     *
     * @param end
     * @throws Exception
     */
    @RequestMapping("/summary1")
    public void summary1(@DateString String end) throws Exception {

        BiDatabaseInfoModel model = this.biDatabaseInfoService.getModelOne(FrameUtil.newHashMap("bs_type", 2),
                DbWREnums.READ);
        Long endDate = FrameUtil.getTime(DateUtil.parseDate(end, DateUtil.PATTERN_yyyyMMdd)) + 86399;
        JdbcTemplate jt = BiUtil.getJdbcTemplate(model);
        Map map = jt.queryForMap(GET_AUTO_INVEST_COUNT, new Object[]{endDate, endDate, endDate});
        System.out.println(FrameUtil.json(map, true));
        super.successData(FrameUtil.newHashMap("summaryVals", map.values()));
    }


    /**
     * 基础数据-自动投标
     *
     * @param end
     * @throws Exception
     */
    @RequestMapping("/summary2")
    public void summary2(@DateString String start, @DateString String end) throws Exception {

        BiDatabaseInfoModel model = this.biDatabaseInfoService.getModelOne(FrameUtil.newHashMap("bs_type", 2),
                DbWREnums.READ);
        Long startDate = FrameUtil.getTime(DateUtil.parseDate(start, DateUtil.PATTERN_yyyyMMdd));
        Long endDate = FrameUtil.getTime(DateUtil.parseDate(end, DateUtil.PATTERN_yyyyMMdd)) + 86399;
        JdbcTemplate jt = BiUtil.getJdbcTemplate(model);
        //投资人数（人）
        int count = jt.queryForObject(GET_INVEST_COUNT, new Object[]{startDate, endDate}, Integer.class);
        //投资金额（元）
        double price = jt.queryForObject(GET_INVEST_PRICE, new Object[]{startDate, endDate}, Double.class);
        //投资人数（人）
        int count1 = jt.queryForObject(GET_INVEST_COUNT_1, new Object[]{startDate, endDate}, Integer.class);
        //投资金额（元）
        double price1 = jt.queryForObject(GET_INVEST_PRICE_1, new Object[]{startDate, endDate}, Double.class);
        //投资人数（人）
        int count2 = jt.queryForObject(GET_INVEST_COUNT_2, new Object[]{startDate, endDate}, Integer.class);
        //投资金额（元）
        double price2 = jt.queryForObject(GET_INVEST_PRICE_2, new Object[]{startDate, endDate}, Double.class);
        super.successData(FrameUtil.newHashMap("summaryVals", FrameUtil.newArrayList(
                NumberUtil.formatNumber(count, NumberUtil.FORMAT_2),
                NumberUtil.formatNumber(price, NumberUtil.FORMAT_4),
                NumberUtil.formatNumber(count1, NumberUtil.FORMAT_2),
                NumberUtil.formatNumber(price1, NumberUtil.FORMAT_4),
                NumberUtil.formatNumber(count2, NumberUtil.FORMAT_2),
                NumberUtil.formatNumber(price2, NumberUtil.FORMAT_4))));
    }


}
